package icehw.cn.bmicalculate;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by pc on 2016/12/15.
 */

public class bmi extends Activity {

    private EditText et_height;
    private EditText et_weight;
    private TextView result;
    private TextView suggestion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bmi);
    }

    public void count(View v){
        et_height = (EditText) findViewById(R.id.height_input);
        et_weight = (EditText) findViewById(R.id.weight_input);
        result = (TextView) findViewById(R.id.result);
        suggestion = (TextView) findViewById(R.id.suggestion);

        String height = et_height.getText().toString();
        String weight = et_weight.getText().toString();

        if (height.isEmpty() || weight.isEmpty()){
            Toast.makeText(this , "输入有误，请重新输入！" , Toast.LENGTH_SHORT).show();
        }else {
            float _height = Float.parseFloat(height);

            if (_height < 230 && _height > 70) {
                float _height_ = _height / 100F;
                float _weight = Float.parseFloat(weight);

                /*
                 *  BMl＜18.5为偏瘦；BMl 在18.5～24.9之间为正常；
                 *  BMl≥25为超重；BMl在25～29.9 之间为1级肥胖；
                 *  BMl＞30～34.9为2级肥胖；BMl＞35～39.9为 3级肥胖；BMl＞40为4级肥胖。
                */

                float bmi = _weight / (_height_ * _height_);
                float _bmi = (float)(Math.round(bmi*10))/10;
                if (_bmi < 18.5) {
                    String _result = "你的BMI指数为：" + _bmi;
                    String _suggestion = "你身体偏瘦，请注意饮食，增加营养！";
                    result.setText(_result);
                    suggestion.setText(_suggestion);
                }
                else if (_bmi >= 18.5 && _bmi < 25) {
                    String _result = "你的BMI指数为：" + _bmi;
                    String _suggestion = "身材比较标准，继续保持！";
                    result.setText(_result);
                    suggestion.setText(_suggestion);
                }
                else if (_bmi >= 25 && _bmi < 30) {
                    String _result = "你的BMI指数为：" + _bmi;
                    String _suggestion = "身体有点偏胖，你平时需要多锻炼一下，减轻体重！";
                    result.setText(_result);
                    suggestion.setText(_suggestion);
                }
                else if (_bmi >= 30 && _bmi < 35) {
                    String _result = "你的BMI指数为：" + _bmi;
                    String _suggestion = "体重超标了，请合理饮食，减肥迫在眉睫！";
                    result.setText(_result);
                    suggestion.setText(_suggestion);
                } else {
                    String _result = "你的BMI指数为：" + _bmi;
                    String _suggestion = "你必须得减肥了，要不然你的身体会不堪重负！";
                    result.setText(_result);
                    suggestion.setText(_suggestion);
                }
            } else {
                Toast.makeText(this, "请输入正确的身高数据", Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void reset(View v){
        et_height = (EditText) findViewById(R.id.height_input);
        et_weight = (EditText) findViewById(R.id.weight_input);
        result = (TextView) findViewById(R.id.result);
        suggestion = (TextView) findViewById(R.id.suggestion);
        et_weight.setText("");
        et_height.setText("");
        result.setText("");
        suggestion.setText("");
    }

}
